<html>
<head><meta charset="utf-8"><title>ref to uninitialized memory · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html">ref to uninitialized memory</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="151292235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151292235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151292235">(Dec 10 2018 at 18:11)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> miri says that the following is ok: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=e68a01cfb3d769ae90690c60e6511eec" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=e68a01cfb3d769ae90690c60e6511eec">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=e68a01cfb3d769ae90690c60e6511eec</a></p>
<p>Does that mean that references to uninitialized memory can be ok ?</p>



<a name="151292692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151292692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> briansmith <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151292692">(Dec 10 2018 at 18:17)</a>:</h4>
<p>I'm pretty sure you have to allow that because lots of code already uses it.</p>



<a name="151292699"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151292699" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> briansmith <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151292699">(Dec 10 2018 at 18:17)</a>:</h4>
<p>Has anybody tried to extend the borrow checker to allow "write-only" borrows?</p>



<a name="151292820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151292820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> briansmith <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151292820">(Dec 10 2018 at 18:18)</a>:</h4>
<p>In fact, there's a lot of existing code that does the same thing, except <code>foo()</code> itself takes a reference and initializes the memory using the reference.</p>



<a name="151404284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404284">(Dec 10 2018 at 21:53)</a>:</h4>
<p>miri doesn't reject all the things I would like it to reject ;) mostly because we haven't decided about the invariants yet</p>



<a name="151404338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404338">(Dec 10 2018 at 21:54)</a>:</h4>
<p>there are two things that code does which are worth discussing: it has an uninitialized integer (I think that should be UB), and it has a reference to an uninitialized integer (I think that should be fine)</p>



<a name="151404386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404386">(Dec 10 2018 at 21:55)</a>:</h4>
<p>but <span class="user-mention" data-user-id="132920">@gnzlbg</span> in general, you shouldnt take "miri accepts it" has "UB-free". there are things that are not implemented, and there are things where the checks are not as aggressive as I could make them because I want to see how the UCG discussions go first.</p>



<a name="151404444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404444">(Dec 10 2018 at 21:56)</a>:</h4>
<p>makes sense</p>



<a name="151404458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404458">(Dec 10 2018 at 21:56)</a>:</h4>
<p>that example is pretty much an idiom that the rsmpi crate uses ~a million times.</p>



<a name="151404610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404610">(Dec 10 2018 at 21:59)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> well <code>mem::uninitialized</code> is going to get deprecated. how would you have written that with <code>MaybeUninit</code>?</p>



<a name="151404633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404633">(Dec 10 2018 at 21:59)</a>:</h4>
<p>the proper way</p>



<a name="151404711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404711">(Dec 10 2018 at 22:00)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> could you write some code? "proper" isn't very explicit ;)</p>



<a name="151404717"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404717" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404717">(Dec 10 2018 at 22:00)</a>:</h4>
<p><code>MaybeUninit</code> has an <code>as_mut_ptr</code> method that makes it trivial to pass "out" parameters to C</p>



<a name="151404726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404726">(Dec 10 2018 at 22:00)</a>:</h4>
<p>oh you just need raw ptrs anyway</p>



<a name="151404732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404732">(Dec 10 2018 at 22:00)</a>:</h4>
<p>yeah then it's probably easy</p>



<a name="151404831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404831">(Dec 10 2018 at 22:02)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span>  <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=d6962e8e6098a191ef97bcd653630232" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=d6962e8e6098a191ef97bcd653630232">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=d6962e8e6098a191ef97bcd653630232</a></p>



<a name="151404840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151404840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151404840">(Dec 10 2018 at 22:02)</a>:</h4>
<p>yeah that looks perfect :)</p>



<a name="151429778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151429778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151429778">(Dec 11 2018 at 08:13)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> so how is my initial example using <code>uninitialized</code> UB in a world with your RFC about <code>&amp;mut T as *mut T</code> ?</p>



<a name="151429835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151429835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151429835">(Dec 11 2018 at 08:14)</a>:</h4>
<p>the user writes <code>&amp;mut T</code>, but that reference is never put into a place, it is never dereferenced, and it is coerced / casted into a raw pointer before the result of the expression it is in is put into a place</p>



<a name="151429848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151429848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151429848">(Dec 11 2018 at 08:15)</a>:</h4>
<blockquote>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> so how is my initial example using <code>uninitialized</code> UB in a world with your RFC about <code>&amp;mut T as *mut T</code> ?</p>
</blockquote>
<p>see the int/float point in <a href="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/54" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/54">https://github.com/rust-rfcs/unsafe-code-guidelines/pull/54</a> ;)</p>



<a name="151429850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151429850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151429850">(Dec 11 2018 at 08:15)</a>:</h4>
<p>if I have my way, <code>let x: i32 = mem::uninitialized();</code> is UB</p>



<a name="151429899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151429899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151429899">(Dec 11 2018 at 08:16)</a>:</h4>
<p>because you can only have uninitailized data inside a union</p>



<a name="151429905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151429905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151429905">(Dec 11 2018 at 08:16)</a>:</h4>
<p>if, OTOH, uninitialized integers are valid, then of course <code>&amp;i32</code> is also valid when it points to something uninitialized, even with the strongest possible rules for integers</p>



<a name="151430152"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151430152" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151430152">(Dec 11 2018 at 08:23)</a>:</h4>
<p>ah ok, because it violates the validity of <code>i32</code>, that makes sense</p>



<a name="151430234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151430234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151430234">(Dec 11 2018 at 08:25)</a>:</h4>
<p>time to have some coffee, i just had a deja-vu</p>



<a name="151431511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151431511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151431511">(Dec 11 2018 at 08:56)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> is a bit of topic here, but I saw your reply in the RFC for the new mir operator, does that mean that if <code>&amp;t as &amp;T as *const T as &amp;T as *const T</code> creates a raw pointer directly ?</p>



<a name="151431692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151431692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151431692">(Dec 11 2018 at 09:01)</a>:</h4>
<p>I wonder if the UCG would need to mention that in the validity of references somehow</p>



<a name="151431730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151431730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151431730">(Dec 11 2018 at 09:02)</a>:</h4>
<p>or whether that's up to the language to specify that coercion chains that create raw pointers are "atomic"</p>



<a name="151432071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151432071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151432071">(Dec 11 2018 at 09:11)</a>:</h4>
<blockquote>
<p>does that mean that if &amp;t as &amp;T as *const T as &amp;T as *const T creates a raw pointer directly ?</p>
</blockquote>
<p>no</p>



<a name="151432119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151432119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151432119">(Dec 11 2018 at 09:12)</a>:</h4>
<p>by saying <code>as &amp;T</code>, you are explicitly requesting a reference, not a raw ptr</p>



<a name="151432131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151432131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151432131">(Dec 11 2018 at 09:12)</a>:</h4>
<p>but that expression would trigger the lint</p>



<a name="151434661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151434661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151434661">(Dec 11 2018 at 10:07)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> so I think it might be worth to consider such an extension</p>



<a name="151434677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151434677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151434677">(Dec 11 2018 at 10:07)</a>:</h4>
<p>there was also the issue of <code>x as _</code> where <code>_: &amp;T</code> creates a reborrow, even though at the end everything is coerced to a raw pointer</p>



<a name="151434831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151434831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151434831">(Dec 11 2018 at 10:10)</a>:</h4>
<p>While those are all different issues, I'm starting to think that it's problematic for <code>as</code> and coercion chains to actually borrow / create references if they end up in a raw pointer</p>



<a name="151434929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151434929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151434929">(Dec 11 2018 at 10:12)</a>:</h4>
<p>I think if you write <code>as &amp;T</code> you asked for a reference pretty clearly, it would be strange not to do that</p>



<a name="151434936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref%20to%20uninitialized%20memory/near/151434936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ref.20to.20uninitialized.20memory.html#151434936">(Dec 11 2018 at 10:12)</a>:</h4>
<p>but <code>as _</code> shouldn't infer a reference type unless necessary (that's <a href="https://github.com/rust-lang/rust/issues/56604" target="_blank" title="https://github.com/rust-lang/rust/issues/56604">#56604</a>)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>